<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form label-width="100px" :model="userInfo">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <UploadImg :staff-photo.sync="userInfo.staffPhoto"></UploadImg>
            <!-- <el-image :src="require('@/assets/common/head.jpg')"></el-image> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="update">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
import { saveUserDetailById } from '@/api/employee.js'

export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    ...mapActions('user', ['getUserInfo']),
    async update () {
      try {
        await saveUserDetailById(this.userInfo)
        // 并且同时更新右上角NavBar上面的头像信息 ===> 根源 : 数据驱动视频===>存在vuex里面 调用vuex里的action方法 重新获取数据
        this.getUserInfo()
        this.$message.success('更新成功')
        // 更新完成返回列表页面
        this.$router.back()
      } catch (error) {
        this.$message.error('更新失败')
      }
    }
  }
}
</script>
